<?php
	class Kelas_tahun_ajaranController extends Controller{
		private $result = array("actionStatus"=>0,"actionMessage"=>"","model"=>array(),"actionError"=>array());	
		public $mainMenu = "Pendataan Kelas";
		public $subMenu = "";
		public $title = "";	
		public $module = "pendataan_kelas";
		public $menuId = "";
		
		public function init() {}
		public function cekPermission(){
			return true;			
		}
		
		public function actionIndex(){
			$this->subMenu = "Kelas";
			$this->layout ="//layouts/ly_blank";
			
			$departments = MDepartment::model()->findAll("is_active = TRUE order by id");
			$department_id ="";
			
			if ($departments == null){ 
				$department_id == "";
			}else{
				$department_id = $department_id == "" ? $departments[0]->id : $department_id;
			}
			
			$tahunAjarans = MTahunAjaran::model()->findAll("department_id=:departmentId and is_active = TRUE order by id",array(":departmentId"=>$department_id));
			$tahun_ajaran_id = "";
			if ($tahunAjarans == null){
				$tahun_ajaran_id == "";
			}else{
				$tahun_ajaran_id = $tahun_ajaran_id == "" ? $tahunAjarans[0]->id : $tahun_ajaran_id;
			}
			
			$data= array(
					"departments" => $departments,
					"department_id"=>$department_id,
					"tahunAjarans"=>$tahunAjarans,
					"tahun_ajaran_id" => $tahun_ajaran_id
			);
			$this->render('index',$data);
		}
		
		public function actiondetailList(){		
			$onlyActive = Yii::app()->request->getParam("activeOnly",false);
			$department_id = Yii::app()->request->getParam("department_id","");
			$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id","");
			
			$condition = "where department_id = :deptId";
			$arrParam = array(":deptId"=>$department_id);			
			if ($onlyActive){
				$condition .= " and is_active = true";
			}
			
			if ($tahun_ajaran_id != ""){
				$condition .= " and tahun_ajaran_id = :tahunAjaranId";
				$arrParam[":tahunAjaranId"] = $tahun_ajaran_id;
			}
			
			$sql ="select * from v_kelas_tahun_ajaran ";
			$sql .= $condition;
			
			$conn = Yii::app()->db;
			$command = $conn->createCommand($sql);
			$rs =$command->query($arrParam);
			$rw = $rs->readAll();
			$this->result["actionStatus"] =0;
			$this->result["actionMessage"] ="";
			$this->result["actionError"] =array();
			$this->result["model"] = $rw;
			
			$this->returnJSON($this->result);
			//header('Content-type:application/json');
			//echo CJSON::encode($this->result);
		}
		
		public function actionedit(){
			$op = Yii::app()->request->getParam("op","add");
			$startDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("start_date","01-Jan-2000")),"Y-m-d");
			$endDate = date_format(date_create_from_format( "d-M-Y", Yii::app()->request->getParam("end_date","01-Jan-2000")),"Y-m-d");
			$status = Yii::app()->request->getParam("is_active",0) === "on"?1:0 ;
			$monthly_fee = Yii::app()->request->getParam("monthly_fee",0);
			$monthly_fee = Utils::parseCurrency($monthly_fee);
			
			$id  = Yii::app()->request->getParam("id",null);
			$tahunAjaranId  = Yii::app()->request->getParam("tahun_ajaran_id",null);
			$kelasId = Yii::app()->request->getParam("kelas_id",null);

			if ($op == "add"){
				//unique for tahun_ajaran_id and kelas_id
				$kelasTahunAjaran = MKelasTahunAjaran::model()->find("tahun_ajaran_id = :tahunAjaranId and kelas_id = :kelasId",array(":tahunAjaranId"=>$tahunAjaranId,":kelasId"=>$kelasId));
				if ($kelasTahunAjaran != null){
					echo Yii::t("label","Data kelas ")."'". $kelasTahunAjaran->kelas->name ."'" . Yii::t("label"," pada tahun ajaran ")."'". $kelasTahunAjaran->tahunAjaran->name ."'". Yii::t("label"," telah terdaftar");
					exit;
				}
					
				
				LoginUser::getPermission("akademik_pendataankelas_kelastahunajaran_add");
				$kelasTahunAjaran = new MKelasTahunAjaran();
				$kelasTahunAjaran->tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id",null);
				$kelasTahunAjaran->kelas_id = Yii::app()->request->getParam("kelas_id",null);
				$kelasTahunAjaran->monthly_fee = $monthly_fee;
				
				$kelasTahunAjaran->wali_kelas_id = Yii::app()->request->getParam("wali_kelas_id",null);
				$kelasTahunAjaran->notes = Yii::app()->request->getParam("notes","");
				$kelasTahunAjaran->is_active=$status;
				if ($kelasTahunAjaran->save()){
					echo "<script>
						alert('Data Sudah di tambahkan...!');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($kelasTahunAjaran->getErrors());
					exit;
				}				
			}else if ($op == "edit"){
				//print_r ($_POST);
				$kelasTahunAjaran = MKelasTahunAjaran::model()->find("id != :id and tahun_ajaran_id = :tahunAjaranId and kelas_id = :kelasId",array(":id"=>$id,":tahunAjaranId"=>$tahunAjaranId,":kelasId"=>$kelasId));
				if ($kelasTahunAjaran != null){
					echo Yii::t("label","Data kelas ")."'". $kelasTahunAjaran->kelas->name ."'" . Yii::t("label"," pada tahun ajaran ")."'". $kelasTahunAjaran->tahunAjaran->name ."'". Yii::t("label"," telah terdaftar");
					exit;
				}
				
				$id = Yii::app()->request->getParam("id","");				
				$kelasTahunAjaran = MKelasTahunAjaran::model()->find("id=:id",array(":id"=>$id));
				$kelasTahunAjaran->kelas_id = Yii::app()->request->getParam("kelas_id",null);
				$kelasTahunAjaran->monthly_fee = $monthly_fee;
				$kelasTahunAjaran->wali_kelas_id = Yii::app()->request->getParam("wali_kelas_id",null);
				$kelasTahunAjaran->notes = Yii::app()->request->getParam("notes","");
				$kelasTahunAjaran->is_active=$status;
				
				if ($kelasTahunAjaran->save()){
					echo "<script>
						alert('".Yii::t("dialog","Perubahan data sudah di simpan...!."). "');					
						window.close();					
					</script>";
					exit;
				}else{
					print_r($kelasTahunAjaran->getErrors());
				}
			}else if ($op == "del"){
				$id = Yii::app()->request->getParam("id","");
				$conn = Yii::app()->db;				
				$kelasTahunAjaran = MKelasTahunAjaran::model()->findByPk($id);				
				if ($kelasTahunAjaran == null){
					$this->result["actionStatus"] =1;
					$this->result["actionMessage"] = Yii::t("dialog", "Id tidak terdaftar !");					
					$this->returnJSON($this->result);
				}else{
					try{
						$kelasTahunAjaran->delete();
						$this->result["actionStatus"] =0;
						$this->result["actionMessage"] = "";
						$this->result["actionError"] = "";						
					}catch (Exception $e){
						$this->result["actionStatus"] =1;
						$this->result["actionMessage"] = Yii::t("dialog", "Data Tidak dapat di hapus !");
						$this->result["actionError"] = $e;
					}
				}
				$this->returnJSON($this->result);
			}			
			
		}
		
		public function actionform(){
			$op = Yii::app()->request->getParam("op","add");
			$callback = Yii::app()->request->getParam("callback","");
			$departments = MDepartment::model()->findAll();
			$department_id = Yii::app()->request->getParam("department_id",$departments[0]->id);			
			$tahunAjarans = MTahunAjaran::model()->findAll("department_id = :deptId and is_active = 1",array(":deptId"=>$department_id));
			$tahun_ajaran_id ="";
			if ($tahunAjarans != null){
				$tahun_ajaran_id = Yii::app()->request->getParam("tahun_ajaran_id",$tahunAjarans[0]->id);
			}
			$arrTingkatan = MTingkatan::model()->findAll("department_id = :deptId and is_active = 1 ",array(":deptId"=>$department_id));
			$tingkatan_id ="";
			if ($arrTingkatan != null){
				$tingkatan_id = Yii::app()->request->getParam("tingkatan_id",$arrTingkatan[0]->id);
			}

			$ssql = "select kelas_id from m_kelas_tahun_ajaran where tahun_ajaran_id = $tahun_ajaran_id";
					
			$arrKelas = MKelas::model()->findAll("tingkatan_id = :tingkatanId and is_active = 1",array(":tingkatanId"=>$tingkatan_id));
			$kelas_id ="";
			if ($arrKelas != null){
				$kelas_id = Yii::app()->request->getParam("kelas_id",$arrKelas[0]->id);
			}

			$data = array("op"=>"add","callback"=>"","id"=>0,"department_id" =>$department_id,"name"=>"","start_date"=>"","end_date"=>"","description"=>"","is_active"=>"1");			
			if ($op == "add"){
				LoginUser::getPermission("akademik_pendataankelas_kelastahunajaran_add");
				$data["op"] = "add";
				$data["callback"] = $callback;
				$data["departments"] = $departments;
				$data["department_id"] = $department_id;				
				$data["tahunAjarans"] = $tahunAjarans;
				$data["tahun_ajaran_id"] = $tahun_ajaran_id;
				$data["arrTingkatan"] = $arrTingkatan;
				$data["tingkatan_id"] = $tingkatan_id;
				$data["arrKelas"] = $arrKelas;
				$data["kelas_id"] = $kelas_id;
				$data["monthly_fee"] = 0;
				$data["wali_kelas_id"] = "";
				$data["wali_kelas_name"] = "";
				
			}else if($op =="edit"){
				LoginUser::getPermission("akademik_pendataankelas_kelastahunajaran_edit");
				$data["op"] = "edit";
				$data["callback"] = $callback;
				$data["departments"] = $departments;				
				$data["tahunAjarans"] = $tahunAjarans;
				$data["arrTingkatan"] = $arrTingkatan;
				$data["arrKelas"] = $arrKelas;
				$id = Yii::app()->request->getParam("id","0");				
				$data["id"] = $id;
				//$rsKelasTahunAjaran = MKelasTahunAjaran::model()->findAll(array("condition"=>"id = $id","order"=>"id"));
				$rsKelasTahunAjaran = MKelasTahunAjaran::model()->findByPK($id);
				if ($rsKelasTahunAjaran != null){
					$data["department_id"] = $rsKelasTahunAjaran->tahunAjaran->department_id;
					$data["tahun_ajaran_id"] = $rsKelasTahunAjaran->tahun_ajaran_id;
					$data["tingkatan_id"] = $rsKelasTahunAjaran->kelas->tingkatan_id;
					$data["kelas_id"] = $rsKelasTahunAjaran->kelas_id;
					$data["monthly_fee"] = $rsKelasTahunAjaran->monthly_fee;
					$data["wali_kelas_id"] = $rsKelasTahunAjaran->wali_kelas_id;
					$data["wali_kelas_name"] = $rsKelasTahunAjaran->waliKelas->name;
				}else{
					echo Yii::t("label","Data tidak di temukan !");
					exit;
				}
			}			
			$this->layout ="//layouts/ly_blank";
			$this->render("form",$data);
		}
				
		protected function beforeRender($view){
			if ($this->subMenu != ""){
				$this->title = $this->mainMenu . " - " .$this->subMenu;
			}
			return true;
		}
		
		/*
		*Action Created by : Michael 2014-02-26
		*/
		public function actionPrint(){ 
			$department_id= Yii::app()->request->getQuery('department_id','1');
			$this->subMenu = "Tahun Ajaran";
			$this->layout ="//layouts/ly_blank";
			//$data=array("department_id"=>"");
			$data = Yii::app()->db->createCommand()
				->select('id, name, start_date, end_date, description, is_active')
				->from('m_tahun_ajaran')
				->where('department_id=:department_id', array(':department_id'=>$department_id))
			->queryAll();		
			$this->render('print',array('data' => $data));
		}
		
		public function actionpopup(){
			$data=array("callback"=>Yii::app()->request->getParam('callback',''));			
			$this->layout ="//layouts/ly_blank";
			$this->render("popup",$data);
		}
	}
?>